/*
 Copyright (C) 2010 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.topisv.api.response;

import java.io.Serializable;

/**
 * Api响应
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @since 0.2, 2010-7-29
 * @version $Id: ApiResponse.java 70 2012-09-08 16:29:34Z zhaofang123 $
 */
public class ApiResponse implements Serializable {
	private static final long serialVersionUID = 1432432432432111L;
	
	// 成功
	public static final int SC_OK				= 1;
	// 失败
	public static final int SC_FAIL				= 0;
	// 授权过期
	public static final int SC_BAD_SESSION		= 2;
	// 业务级错误
	public static final int SC_BUSINESS_ERR 	= 10;
	// 宝贝未找到
	public static final int SC_ITEM_NOT_FOUND 	= 11;
	// 宝贝属性错误
	public static final int SC_ITEM_BAD_ATTR 	= 12;
	
	// -----------------------------------------------------------------------------------
	
	public static final ApiResponse OK 				= new ApiResponse(SC_OK, null, null);
	public static final ApiResponse FAIL 			= new ApiResponse(SC_FAIL, "未知错误", null);
	public static final ApiResponse BAD_SESSION 	= new ApiResponse(SC_BAD_SESSION, "授权已过期", null);
	public static final ApiResponse ITEM_NOT_FOUND 	= new ApiResponse(SC_ITEM_NOT_FOUND, "未找到宝贝", null);
	
	/**
	 * @param errorRsp
	 * @return
	 */
	public static ApiResponse valueOfFail(ErrorRsp errorRsp) {
		return valueOfFail(errorRsp.toLocalizedMessage());
	}
	
	/**
	 * @param message
	 * @return
	 */
	public static ApiResponse valueOfFail(String message) {
		return valueOf(SC_FAIL, message, null);
	}
	
	/**
	 * @param response
	 * @return
	 */
	public static ApiResponse valueOfOk(Object response) {
		return valueOf(SC_OK, null, response);
	}
	
	/**
	 * @param code
	 * @param message
	 * @param response
	 * @return
	 */
	public static ApiResponse valueOf(int code, String message, Object response) {
		return new ApiResponse(code, message, response);
	}
	
	// -----------------------------------------------------------------------------------
	
	final private int code;
	final private String message;
	private Object response;
	
	/**
	 * @param code
	 * @param message
	 * @param response
	 */
	public ApiResponse(int code, String message, Object response) {
		this.code = code;
		this.message = message;
		this.response = response;
	}
	
	/**
	 * @return
	 */
	public boolean hasError() {
		return getCode() != SC_OK;
	}
	
	/**
	 * @return
	 */
	public int getCode() {
		return code;
	}
	
	/**
	 * @return
	 */
	public String getMessage() {
		return message;
	}
	
	/**
	 * @return
	 */
	public Object getResponse() {
		return response;
	}
	
	@Override
	public String toString() {
		return code + "#" + message;
	}
}
